<template>
  <el-select
    ref="domRef"
    v-model="formData[_info.field]"
    v-loading="loading"
    :element-loading-spinner="loadingSpinner"
    element-loading-svg-view-box="0,0, 1024, 1024"
    :clearable="_rules.required ? false : true"
    :filterable="_rules.filterable || false"
    :multiple="_info.field_value_type.includes('[]')"
    :multiple-limit="_rules.max_array || 0"
    :placeholder="_info.placeholder || '请选择'"
    :loading="loading"
    :disabled="_info.disabled || false"
    @change="handleChange"
  >
    <el-option
      v-for="item in optionList"
      :key="item.id"
      :label="item.name"
      :value="item.id"
      :disabled="item.disabled"
    />
  </el-select>
</template>

<script setup>
import { getInfo, getRules, changeSelect, getOptions } from '../const';
import { loadingSpinner } from '@/assets/js/loadingIcon.js';

const props = defineProps({
  formInfo: {
    type: Object,
    default: () => {}
  },
  formData: {
    type: Object,
    default: () => {}
  },
  TrendsFormRef: {
    type: Object,
    default: null
  },
  topExpose: {
    type: Object,
    default: () => {}
  }
});

const _info = computed(() => getInfo(props.formInfo));
const _rules = computed(() => getRules(props.formInfo));

// 拿到下拉列表
const optionList = ref([]);
const loading = ref(false);
getOptions({ optionList, loading, props });

let oldValue = undefined;
watch(
  () => props.formData[_info.value.field],
  (newVal, oldVal) => (oldValue = oldVal),
  { deep: true, immediate: true }
);

// 选择事件
const domRef = ref();
function handleChange() {
  setTimeout(() => {
    changeSelect(props.formInfo, props.formData, {
      oldValue,
      domRef,
      topExpose: props.topExpose
    });
  }, 0);
}
</script>

<style lang="scss" scoped></style>
